#!/usr/bin/perl

require "./quotamin-lib.pl";
&ReadParse();

my (@gr, $btime, $bunit, $dir, $lable, $error_msg, $str_unit);
my (@temp);
my ($logminor, $logpriority, $event, $formatstr);

$dir = $in{"checkeddir"};
$lable = $in{"lable"};
$bunit = $in{"bunit"};
$btime = $in{"btime"};

$error_msg = "";

#bunit = seconds not support by quota soft ware
#change seconds to minutes
if(($bunit + 0) == 0 )
{
	$str_unit = "minutes";
	$btime = $btime / 60;
	$btime = sprintf "%.0f", $btime;
	$bunit = 1;
}
if(($bunit + 0) == 1)
{
	$str_unit = "minutes";
}
if(($bunit + 0) == 2)
{
	$str_unit = "hours";
}
if(($bunit + 0) == 3)
{
	$str_unit = "days";
}

if(!$btime)
{
	$error_msg = $text{"error_msg_input_not_num"};
}

unless($error_msg)
{
	$error_msg = &check_input_not_integer($btime);

	
	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'quotamin_naslog_event_savedefault_failed_by_num_err_format'};
		$formatstr = sprintf $text{'quotamin_naslog_savedefault_failed_by_num_err_format'}, $lable;
	}
}

unless($error_msg)
{
	if(!$dir)
	{
		$error_msg = $text{"error_msg_cgi_not_pumit"};
	}
	if(!defined($bunit))
	{
		$error_msg = $text{"error_msg_cgi_not_pumit"};
	}
}

if($error_msg)
{
	#&header($text{'index_title'}, "", undef, 0, 1);
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}

	&error_setup($text{"error_title_save_grace"});
	&error($error_msg);
	#&footer("/", $text{'index'});
}
else
{
	&foreign_require("quota", "quota-lib.pl");
	@gr = &foreign_call("quota", "get_user_grace", $dir);

	$gr[0] = $btime;
	$gr[1] = $bunit;
	#system("./aaa.pl path=$module_root_directory fs=$dir bt=$gr[0] bu=$gr[1] 2>/dev/null");
	my $out = &ez_system("./aaa.pl", "path=$module_root_directory", "fs=$dir", "bt=$gr[0]", "bu=$gr[1]");

	$error_msg = &is_savegrace_failed($dir, $gr[0], $gr[1]);
	
	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_crit'};
		$event = $text{'quotamin_naslog_event_savegrace_failed_in_progress'};
		$formatstr = sprintf $text{'quotamin_naslog_savegrace_failed_in_progress'}, $lable;
		&nas_syslog($logminor, $logpriority, $event, $formatstr);

		&error_setup($text{"error_title_save_grace"});
		&error($error_msg);
	}
	else
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_info'};
		$event = $text{'quotamin_naslog_event_savegrace_success'};
		$formatstr = sprintf $text{'quotamin_naslog_savegrace_success'}, $lable, $btime.$str_unit;
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}


	&redirect("index.cgi");
}
